package com.basemanage.controller.appraise;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.basemanage.core.controller.JCBaseController;
import com.basemanage.core.util.DateUtils;
import com.basemanage.core.util.FileUtils;
import com.basemanage.core.view.InvokeResult;
import com.basemanage.service.AppVersionService;
import com.jfinal.kit.PropKit;
import com.jfinal.upload.UploadFile;

public class AppraiseController extends JCBaseController {
	
	public void index() {
		render("index.html");
	}
	
	/*
	 * 添加幻灯片
	 */
	/**
	 * 跳转添加页面
	 */
	public void add(){
//		Integer id=getParaToInt("id");
//		String action="add";
//		if(id!=null){
//			this.setAttr("item", AppVersionService.dao.findById(id));
//			action="edit";
//		}
//		this.setAttr("action", action);
		render("add.html");
	}
	
	/*
	 * 上传图片 
	 */
	public void uploadApk() {
		
		String dataStr=DateUtils.format(new Date(), "yyyyMMddHHmm");
		List<UploadFile> flist = this.getFiles("/temp", 1024*1024*50);
		Map<String,Object> data=new HashMap<String, Object>();
		if(flist.size()>0){
			UploadFile uf=flist.get(0);
			String fileUrl="apk/"+dataStr+"/"+uf.getFileName();
			String newFile=PropKit.get("uploadPath")+fileUrl;
			FileUtils.mkdir(newFile, false); 
			FileUtils.copy(uf.getFile(), new File(newFile), BUFFER_SIZE);
			uf.getFile().delete();
			data.put("fileUrl",fileUrl);
			renderJson(data);
		}
	}
	
	public void upload(){
		InvokeResult res = new InvokeResult();
        try {
            UploadFile file = getFile();
            System.out.println("--------file--------");
            File delfile = new File(file.getUploadPath()+"\\"+file.getFileName());
            System.out.println("=========="+delfile.getPath());
            Map<String ,String> map = new HashMap<String, String>();
            map.put("filePath", delfile.getPath());
            map.put("fileSize", delfile.length()/1024+"");
            res = InvokeResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
//            resp.setMsg("");
            res = InvokeResult.failure("文件上传失败");
        }
        
        renderJson(res);
    }

}
